<?php
/**
 * Created by zengwei.
 * Date: 2019/10/11
 * Time: 14:07
 * Function:
 */

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Http\Requests\Api\UserLoginRequest;
use App\Http\Requests\Api\UserProfileRequest;
use App\Http\Requests\Api\UserRegisterRequest;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Tymon\JWTAuth\JWT;

class UserController extends Controller
{
    /**
     * Created by zengwei.
     * Date: 2019/10/24
     * Time: 15:57
     * Function:信息
     * @return \Illuminate\Http\JsonResponse
     *
     */
    public function my()
    {
        $user = Auth::guard('api')->user();
        $user = $user->only(['id', 'name', 'avatar', 'email','favorite_coffees','flavor_notes','profile_visibility','state','city']);
        return response()->json(successDataFormat($user));
    }

    /**
     * Created by zengwei.
     * Date: 2019/10/24
     * Time: 15:57
     * Function:登录
     * @param UserLoginRequest $request
     * @return \Illuminate\Http\JsonResponse
     *
     */
    public function login(UserLoginRequest $request)
    {
        $user = User::where('account', $request->input('account'))->first();
        if (!$user) {
            return response()->json(['message' => '账号不存在'], 400);
        }
        $token = \JWTAuth::fromUser($user);
        return response()->json(successDataFormat(['token' => $token]));
    }

    /**
     * Created by zengwei.
     * Date: 2019/10/24
     * Time: 15:57
     * Function:注册
     * @param UserRegisterRequest $request
     * @return \Illuminate\Http\JsonResponse
     * * @throws \Exception
     */
    public function register(UserRegisterRequest $request)
    {
        $user = new User([
            'name' => $request->input('name'),
            'account' => $request->input('account'),
            'email' => $request->input('email'),
            'password' => bcrypt($request->input('password'))
        ]);
        try {
            $user->save();
            $token = \JWTAuth::fromUser($user);
            return response()->json(successDataFormat(['token' => $token]));
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }

    }

    /**
     * Created by zengwei.
     * Date: 2019/10/24
     * Time: 15:57
     * Function:注销
     * @return \Illuminate\Http\JsonResponse
     *
     */
    public function logout()
    {
        Auth::guard('api')->logout();
        return response()->json(successDataFormat());
    }

    public function profile(UserProfileRequest $request){
        $user = Auth::guard('api')->user();
        try{
            $profileVisibility = $request->input('profile_visibility',true);
            $favoriteCoffees = $request->input('favorite_coffees');
            $flavorNotes = $request->input('flavor_notes');
            $city = $request->input('city');
            $state = $request->input('state');
            $user->profile_visibility = $profileVisibility;
            if($favoriteCoffees){
                $user->favorite_coffees = $favoriteCoffees;
            }
            if($flavorNotes){
                $user->flavor_notes = $flavorNotes;
            }
            if($city){
                $user->city = $city;
            }
            if($state){
                $user->state = $state;
            }
           $user->save();
            return response()->json(successDataFormat());
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }

    }

}
